package ui.activity.home.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.AreaBean;
import Bean.KnowledgeParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.biz.AreaBiz;
import ui.activity.home.contract.AreaContract;

/**
 * Created by chenjian on 19/2/26
 */

public class AreaPresenter implements AreaContract.Presenter {

    AreaContract.View view;
    AreaBiz areaBiz;
    List<AreaBean.DataBean.ItemsBean> list = new ArrayList<>();


    @Inject
    public AreaPresenter(AreaContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        areaBiz = (AreaBiz) biz;
    }


    @Override
    public void getAreaList(boolean isRefresh, int type) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        KnowledgeParamBean knowledgeParamBean = new KnowledgeParamBean();
        knowledgeParamBean.setType(type);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(knowledgeParamBean));
        areaBiz.getAreaList(requestBody, new BaseBiz.Callback<AreaBean.DataBean>() {
            @Override
            public void onSuccess(AreaBean.DataBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(AreaBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });

    }

}
